local Task = class("Task")

function Task:ctor()
	self.id = 0
	self.type = nil
	self.state = nil
	self.taskProgress = nil
	self.acceptStamp = 0
	self.completeStamp = 0
	self.mineBattleID = nil
	self.Pos = nil
	self._rewardsList = {}

	-- 配表中数据
	self.conf = nil
end

function Task:read(value)
	self.id = value.id
	self.type = value.type
	self.state = value.state
	self.acceptStamp = value.acceptStamp
	self.completeStamp = value.completeStamp
	self.Pos = value.Pos or 0
	if value.taskProgress then
		self.taskProgress = {}
		for i,v in ipairs(value.taskProgress) do
			local taskProgress = {}
			taskProgress.Type = v.Type
			taskProgress.Num = v.Num
			taskProgress.NeedNum = v.NeedNum
			if v.Objects then
				taskProgress.Objects = {}
				for i1,v1 in ipairs(v.Objects) do
					table.insert(taskProgress.Objects,v1)
				end
			end
			table.insert(self.taskProgress,taskProgress)
		end
	end

	if value.mineBattleID then
		self.mineBattleID = {}
		for i,v in ipairs(value.mineBattleID) do
			table.insert(self.mineBattleID,v)
		end
	else
		self.mineBattleID = nil
	end

	self._rewardsList = {}
	for i,v in ipairs( value.finishReward ) do
		local rewardTemp = UD:parseLootToBagItem( v )
		table.insert( self._rewardsList, rewardTemp )
	end

	self.status = self:convertStatus()
	self.taskType = self:convertType()
	-- print("******************** Task:read *****************")
	-- print("Task:read ----- self.id = ",self.id)
	-- print("Task:read ----- self.type = ",self.type)
	-- print("Task:read ----- self.state = ",self.state)
	-- print("Task:read ----- self.acceptStamp = ",self.acceptStamp)
	-- print("Task:read ----- self.completeStamp = ",self.completeStamp)
	-- print("Task:read ----- self.Pos = ",self.Pos)
	-- dump(self.taskProgress)
	-- dump(self.mineBattleID)

	self.conf = GD:queryTaskById(self.id)
end


function Task:initWithId(id)
	if not id then
		return
	end
	self.id = id
	self.conf = GD:queryTaskById(id)
	assert(self.conf ~= nil, "self.conf is nil")
end
-- 返回奖励
function Task:getRewards()
	return self._rewardsList
end

function Task:getPos()
	return self.Pos
end

function Task:setShareData(shareData)
    self._shareData = shareData
end
function Task:getShareData()
    return self._shareData
end

function Task:getBattleIds()
	return self.mineBattleID
end

function Task:getDefaultProgress()
	if self.taskProgress then
		return self.taskProgress[1]
	end
	return nil
end

function Task:getDefaultDone()
	if self:getDefaultProgress() then
		return self:getDefaultProgress().Num
	end
	return 0
end

function Task:getDefaultTotal()
	if self:getDefaultProgress() then
		return self:getDefaultProgress().NeedNum
	end
	return 0
end

function Task:getId()
	return self.id
end

function Task:getConfig()
	return self.conf
end

function Task:getName()
    return L(self.conf.name)
end

function Task:getContent()
    return L(self.conf.content or "")
end


function Task:isUnlock()
	return self.state == 0
end

function Task:isTaken()
	return self.state == 1
end

function Task:isFinished()
	return self.state == 2
end

function Task:isRewarded()
	return self.state == 3
end

function Task:isHangup()
	return self.state == 4
end

function Task:isFaield()
	return self.state == 5
end

function Task:isMain()
	return self.type == 0
end

function Task:isSide()
	return self.type == 1
end

function Task:isDaily()
	return self.type == 2
end

function Task:isWeekly()
	return self.type == 3
end

function Task:isBounty()
	return self.type == 4
end

function Task:isEliteMatch()
	return self.type == 8 or self.type == 10
end

function Task:isBroadCastSystem()
	return self.conf.broadcastSystem == "1"
end

function Task:convertStatus()
	local status
	if self.state == 0 then
		status = "unlock"
	elseif self.state == 1 then
		status = "taken"
	elseif self.state == 2 then
		status = "accomplished"
	elseif self.state == 3 then
		status = "rewarded"
	elseif self.state == 4 then
		status = "hangup"
	elseif self.state == 5 then
		status = "failed"
	end
	return status
end

function Task:convertType()
	local tType
	if self.type == 0 then
		tType = "main"
	elseif self.type == 1 then
		tType = "side"
	elseif self.type == 2 then
		tType = "daily"
	elseif self.type == 3 then
		tType = "weekly"
	elseif self.type == 4 then
		tType = "bounty"
	end
	return tType
end

function Task:getTaskTypeDesc()
	if self:isMain() then
		return L("task_main_desc")
	end
	if self:isSide() then
		return L("task_branch_desc")
	end
	if self:isDaily() then
		return L("task_daily_desc")
	end
	if self:isBounty() then
		return L("task_gold_desc")
	end
	if self:isWeekly() then
		return L("task_weekly_desc")
	end
	return ""
end

function Task:checkTaskIconPath()
	self.taskIconType = 1
	self.taskIconPath = nil
	self.taskIconBg = nil
	self.taskIconData = nil -- BaseIcon 数据

	if not self.conf then return end
	local taskIcon = self.conf.taskIcon
	if not taskIcon then return end

    -- taskIcon = "2|2|7{{.Class}}20113"

    local arrValue = string.split(taskIcon, "|")
    if #arrValue <= 1 then
        self.taskIconPath = GD:getImagePath(Res.taskIconPath, arrValue[1])
        return
    end

    if tonumber(arrValue[1]) == 1 then
        self.taskIconPath = GD:getImagePath(Res.taskIconPath, arrValue[2])
        return
    end

    if tonumber(arrValue[1]) == 2 and #arrValue >= 3 then
    	self.taskIconType = 2
    	local str = ""
    	local count = #arrValue
    	for i = 2, count do
    		str = str .. tostring(arrValue[i])
    		if i < count then
    			str = str .. "|"
    		end
    	end

        self.taskIconData = UD:parseItemFromString(str)
        return
    end
end

function Task:getTaskIconPath()
	if not self._alreayGet then
		self._alreayGet = true
		self:checkTaskIconPath()
	end 
end

-- 设置任务的icon
-- icon 任务本身的icon 不能为空
function Task:setTaskIcon(icon, bg, iconRender, iconDefault)
	self:getTaskIconPath()
	
	icon:setVisible(false)
	if bg then
		bg:setVisible(false)
	end
	if iconRender then
		iconRender:setVisible(false)
	end
	if iconDefault then
		iconDefault:setVisible(true)
	end

	if self.taskIconType == 1 then
		if self.taskIconPath then
        	icon:setVisible(true)
        	icon:loadTexture(self.taskIconPath)
        	if iconDefault then
				iconDefault:setVisible(false)
			end
        end
        if bg then
        	bg:setVisible(true) -- 默认的bg，在每个模块自己设置
        	if self.taskIconBg then
        		bg:loadTexture(self.taskIconBg)
        	end
    	end
    elseif self.taskIconType == 2 then
    	if iconRender then
    		iconRender:setVisible(true)
    		iconRender:setData(self.taskIconData)
    		if iconDefault then
				iconDefault:setVisible(false)
			end
    	end
    end

end

-- 获取配表中的奖励
function Task:getDefaultRewards()
	local rewards = {}
	local AryList = string.splitTwice(self.conf.finishRewards, {";", "|"})
	for i, v in ipairs(AryList) do
		local dropId = tonumber(v[1])
		local reward = UD:parseDropIDToBagItem(dropId)
		table.insert(rewards, reward)
	end
	return rewards
end

return Task
